// packageB/member/enterprise/enterprise_apply/enterprise_apply.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: "", //当前申请状态
    config_status: "", //后台申请条件
    config2: {}, //后台条件2
    config3: {}, //后台条件3
    income_name_text: "",
    username: "",
    password: "",
    password_again: "",
    apply_background:"",
    upload_flag:true,
    apply_switch: false, //判断子平台角色时账号密码不填 - #11419
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: "",
      config_status: "",
    });
    this.checkrouter();
    this.getData();
    this.customizeIncome();
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == "enterprise_apply") {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          wx.showToast({
            title: "未开启推广权限",
            duration: 1000,
            icon: "none",
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            },
          });
          return;
        }
      }
      this.setData({
        apply_switch: basic_info.plugin_setting.sub_platform?.apply_switch ?? false
      })
    } catch (e) {
      console.log(e);
    }
  },
  onChange(e) {
    let inp = e.detail;
    this.setData({
      username: inp,
    });
  },
  onChange_p(e) {
    let inp = e.detail;
    this.setData({
      password: inp,
    });
  },
  onChange_p2(e) {
    let inp = e.detail;
    this.setData({
      password_again: inp,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  //获取申请状态以及商城配置信息
  getData() {
    let that = this;
    let json = {};
    let urlStr = app.getNetAddresss("plugin.merchant.frontend.get-info");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            status: res.data.status,
            config_status: res.data.config_status,
            config2: res.data.config2,
            config3: res.data.config3,
            independent_backend: res.data.independent_backend,
          });
          if(res.data.apply_background == ''){
            that.setData({upload_flag:false})
          }else{
            that.setData({apply_background:res.data.apply_background})
          }
          if (res.data.lang) {
            that.setData({
              title: res.data.lang.title,
              merchant_people: res.data.lang.merchant_people,
            });
            wx.setNavigationBarTitle({
              title: `${that.data.merchant_people}申请`,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //自定义提现收入语言
  customizeIncome() {
    try {
      var value = wx.getStorageSync("mailLanguage");
      if (value) {
        let mailLanguage = JSON.parse(value);
        // Do something with return value
        this.setData({
          income_name_text: mailLanguage.income.income_name,
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  //去招商中心
  goEnterproseCentre() {
    wx.navigateTo({
      url: "/packageB/member/enterprise/enterprise_index/enterprise_index",
    });
  },
  //提交数据
  submit() {
    let that = this;
    let json = {
      config_status: this.data.config_status,
      user: {
        username: that.data.username,
        password: that.data.password,
        password_again: that.data.password_again,
      },
    };
    let urlStr = app.getNetAddresss("plugin.merchant.frontend.merchant-apply.staff");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            status: res.data.status,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //去商品页面
  goToGoods(e) {
    let goods_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + goods_id,
    });
  },
  //去商城
  goHome() {
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },
});
